package com.wlyuan.open.scanner.domain.finance.pay;

import com.wlyuan.open.scanner.dao.entity.BalanceCheckDetailsDO;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import com.wlyuan.open.utils.LongUtils;
import lombok.var;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Optional;

/**
 * @author lijing
 * @description
 */
public class BillPayConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "checkDetailid"),
                @Mapping(target = "sourceKey", source = "checkDetailid"),
                @Mapping(target = "createTime", source = "checkDetailbilltime"),
                @Mapping(target = "updateTime", source = "checkDetailbilltime"),
                @Mapping(target = "createBy", source = "checkDetailcreator"),
                @Mapping(target = "updateBy", source = "checkDetailcreator"),

                @Mapping(target = "billId", source = "checkDetailownerid"),
                @Mapping(target = "waybillId", source = "checkDetailorderid"),
                @Mapping(target = "combineId", source = "ownerCombinedOrderId"),
                @Mapping(target = "type", source = "checkDetailsertype"),
                @Mapping(target = "money", source = "checkDetailbillcost"),
                @Mapping(target = "tenantId", source = "checkDetailcompanyid"),
        })
        @BaseMapping
        BillPay convert(BalanceCheckDetailsDO source);

        @AfterMapping
        default void after(@MappingTarget BillPay target, BalanceCheckDetailsDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
        }

        List<BillPay> convert(List<BalanceCheckDetailsDO> list);

        @Mappings({
                @Mapping(target = "tenantId", source = "upTenantId"),
                @Mapping(target = "payment.mode", source = "checkDetailpaytype"),
                @Mapping(target = "payment.bankAccount", source = "checkDetailbankno"),
                @Mapping(target = "payment.bizAt", source = "checkDetailbilltime"),
                @Mapping(target = "payment.bizBy", source = "checkDetailcreator"),
                @Mapping(target = "payment.desc", source = "checkDetailcomments"),
        })
        BillPayUpStream upConvert(BalanceCheckDetailsDO source);

        @Mappings({
                @Mapping(target = "tenantId", source = "downTenantId"),
                @Mapping(target = "collect.mode", source = "checkDetailpaytype"),
                @Mapping(target = "collect.bankAccount", source = "checkDetailbankno"),
                @Mapping(target = "collect.bizAt", source = "checkDetailbilltime"),
                @Mapping(target = "collect.bizBy", source = "checkDetailcreator"),
                @Mapping(target = "collect.desc", source = "checkDetailcomments"),
        })
        BillPayDownStream downConvert(BalanceCheckDetailsDO source);

    }

    public static List<BillPay> convert(List<BalanceCheckDetailsDO> creation) {
        return CONVERT.convert(creation);
    }

    public static BillPay convert(BalanceCheckDetailsDO source, Long down) {
        var convert = CONVERT.convert(source);
        Optional.ofNullable(convert.getEcho()).ifPresent(h -> {
            h.put("upKey", source.getCheckDetailid());
            if (LongUtils.notNullLong(down)) {
                h.put("downKey", down);
            }
        });
        return convert;
    }

    public static BillPayUpStream upConvert(BalanceCheckDetailsDO source) {
        return CONVERT.upConvert(source);
    }

    public static BillPayDownStream downConvert(BalanceCheckDetailsDO source) {
        return CONVERT.downConvert(source);
    }
}
